# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np
import matplotlib.pyplot as pyplot
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import sklearn.metrics as metrics

data = pd.read_csv('../diabetes.csv')

## 各个数据的相关性不高，并且基本全部是 int64 和 float64 ， 直接对 x_tain 数据的标准化 , y 因为是 0，1 不做
X_train = data.drop("Outcome", axis=1)
y_train = data["Outcome"]

ss_X = StandardScaler()
X_train_trans = ss_X.fit_transform(X_train)

# 拆分 20% 数据作为测试集
X_train_part, X_test, y_train_part, y_test = train_test_split(X_train_trans, y_train, train_size=0.8, random_state=0)

## turn back to DataFrame
X_train_part = pd.DataFrame(data=X_train_part, columns=X_train.columns)
X_test_part = pd.DataFrame(data=X_test, columns=X_train.columns)

from sklearn.grid_search import GridSearchCV
from sklearn.cross_validation import cross_val_score
from sklearn.svm import SVC

# loss = cross_val_score(svc, X_train_part, y=y_train_part, scoring="neg_log_loss", cv=5)
# print("Liner SVM")
# print 'logloss of each fold is: ', -loss
# print'cv logloss is:', -loss.mean()
def fit_grid_point_RBF(C, gamma, X_train, y_train, X_val, y_val):
    # 在训练集是那个利用SVC训练
    SVC3 = SVC(C=C, kernel='rbf', gamma=gamma)
    SVC3 = SVC3.fit(X_train, y_train)

    # 在校验集上返回accuracy
    accuracy = SVC3.score(X_val, y_val)

    print("accuracy: {}".format(accuracy))
    return accuracy


#需要调优的参数
C_s = np.logspace(-2, 2, 5)# logspace(a,b,N)把10的a次方到10的b次方区间分成N份
gamma_s = np.logspace(-2, 2, 5)

accuracy_s = []
for i, oneC in enumerate(C_s):
    for j, gamma in enumerate(gamma_s):
        tmp = fit_grid_point_RBF(oneC, gamma, X_train_part, y_train_part, X_test_part, y_test)
        accuracy_s.append(tmp)

accuracy_s1 =np.array(accuracy_s).reshape(len(C_s),len(gamma_s))
x_axis = np.log10(C_s)
for j, gamma in enumerate(gamma_s):
    pyplot.plot(x_axis, np.array(accuracy_s1[:,j]), label = ' Test - log(gamma)' + str(np.log10(gamma)))

pyplot.legend()
pyplot.xlabel( 'log(C)' )
pyplot.ylabel( 'accuracy' )
# pyplot.savefig('RBF_SVM_Otto.png' )
pyplot.show()
